package snooker.scoreboard;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.util.Log;

public class Match {

	private static final String LOG_TAG = "Match";
	
	public static final int PLAYER1 = 1;
	public static final int PLAYER2 = 2;
	
	private static Match match;
	
	private List<Frame> frames;
	private String player1Name;
	private String player2Name;
	private int player1FrameWins;
	private int player2FrameWins;
	private Frame actFrame;
	private Date startDate;
	private String fileName;
	
	private Match(int maxReds, String player1Name, String player2Name) {
		frames = new LinkedList<Frame>();
		this.player1Name = player1Name;
		this.player2Name = player2Name;
		player1FrameWins = 0;
		player2FrameWins = 0;
		actFrame = new Frame(1,maxReds);
		startDate = new Date();
	}

	public static Match getInstance() {
		if (match == null) {
			match = new Match(Settings.getInstance().getMaxReds(), "Player1", "Player2");
		}
		return match;
	}
	
	public void newMatch(String fileName, List<Frame> frames) {
		this.frames = frames;
		this.fileName = fileName;
		player1FrameWins = 0;
		player2FrameWins = 0;
		for (Frame f: frames) {
			if (f.getPlayer1Score() > f.getPlayer2Score()) {
				player1FrameWins++;
			} else if (f.getPlayer2Score() > f.getPlayer1Score()) {
				player2FrameWins++;
			}
 		}
		actFrame = new Frame(frames.size() + 1, Settings.getInstance().getMaxReds());
		startDate = new Date();
	}
	
	public void newMatch() {
		fileName = null;
		frames = new LinkedList<Frame>();
		player1FrameWins = 0;
		player2FrameWins = 0;
		actFrame = new Frame(1,Settings.getInstance().getMaxReds());
		startDate = new Date();
	}
	
	public String getPlayer1Name() {
		return player1Name;
	}

	public String getPlayer2Name() {
		return player2Name;
	}

	public void setPlayer1Name(String player1) {
		this.player1Name = player1;
	}

	public void setPlayer2Name(String player2) {
		this.player2Name = player2;
	}

	public void newFrame(boolean saveAct, int numberOfReds) {
		if (saveAct) {
			saveActFrame();
		}
		Frame newFrame = new Frame(frames.size() + 1, numberOfReds);
		actFrame = newFrame;
		//return actFrame;
	}
	
	public void saveActFrame() {
		if (actFrame.getHistory().getHistory().size() > 0) {
			frames.add(actFrame);
			calcFrameWins();
		}
	}
	
	public List<Frame> getFrames() {
		return frames;
	}
	
	public int getPlayer1FrameWins() {
		return player1FrameWins;
	}

	public int getPlayer2FrameWins() {
		return player2FrameWins;
	}

	public Frame getActFrame() {
		return actFrame;
	}
	
	private void calcFrameWins() {
		int a = 0;
		int b = 0;
		for (Frame f: frames) {
			if (f.getPlayer1Score() > f.getPlayer2Score()) {
				a++;
			} else if (f.getPlayer1Score() < f.getPlayer2Score()) {
				b++;
			}
		}
		player1FrameWins = a;
		player2FrameWins = b;
	}
	
	public Date getStartDate() {
		return startDate;
	}
	
	public String getFileName() {
		return fileName;
	}
	
	public void debug() {
		Log.i(LOG_TAG, "****************** Match **********************");
		if (fileName != null) {
			Log.i(LOG_TAG, "fileName=" + fileName);
		}
		Log.i(LOG_TAG, "****************** ActFrame **********************");
		actFrame.debug();
		Log.i(LOG_TAG, "****************** History **********************");
		for (Frame f: frames) {
			f.debug();
		}
	}
}
